﻿using UnityEngine;
using System.Collections;



public class BallsMgr : ZT_Object
{

    public static BallsMgr ins
    {
        get
        {
            return BallsMgr.GetInstance();
        }
    }

    private static BallsMgr _ins = null;

    public static BallsMgr GetInstance()
    {
        if (_ins == null)
        {
            _ins = new BallsMgr();
        }
        return _ins;
    }


    public Ball GetOneBallByNo(int no)
    {
        foreach (Ball b in objs)
        {
            if (b.no == no && b.IsValid()) return b;
        }
        return null;
    }

    public Ball GetTheMaxOne(int no)
    {
        Ball ret = this.GetOneBallByNo(no);

        foreach (Ball b in objs)
        {
            //   Debug.Log(b.GetHashCode());
            if (b.IsInValid()) continue;

            if (b.no == no)
            {

                if (b.value > ret.value)
                {
                    return b;

                }
            }
        }
        return ret;
    }

    public ArrayList GetBallsByNo(int no)
    {
        ArrayList ret = new ArrayList();

        foreach (Ball  b in objs)
        {
            if (b.no == no)
            {
                ret.Add(b);
            }
        }
        return ret;
    }




    public override void OnExit()
    {
        _ins.objs.Clear();
        _ins = null;
        base.OnExit();
    }

    Ball self = new Ball();

    private int rank = 0;

    public ArrayList objs = new ArrayList();


    public int me_no = 0;

    public Ball GetSelfBalls()
    {
        return self;
    }
    public override void UpdateMS()
    {

        foreach (Ball b in objs)
        {
            b.UpdateMS();
        }
    }

    public Ball Get(int no)
    {

        foreach (Ball b in objs)
        {
            if (b.no == no) return b;
        }


        return null;
    }
    public ArrayList GetAllBalls()
    {
        return objs;
    }

    public Ball CreateBalls(int no, float x, float y, string name)
    {
        Ball balls = new Ball();
        if (no == me_no && self.value == 1) self = balls;

        balls.no = no;
        balls.x = x;
        balls.y = y;
        balls.name = name;

        Debug.Log("[BallsMgr]:Create a New Balls No=" + no + " me_no:" + this.me_no + "x=" + x + "y=" + y + " name=" + name);
        this.Add(balls);


        ViewMgr.Create<ViewBall>(balls);

        return balls;
    }

    public void CreateSplit(int no)
    {
        ArrayList list = this.GetBallsByNo(no);

        foreach (Ball b in list)
        {
            if (b.value < 20) continue;

            Ball son = BallsMgr.ins.CreateBalls(b.no, b.x , b.y , b.name);

      
            son.x = b.x + b.radis*2 * b.x_delta;
            son.y = b.y + b.radis*2 * b.y_delta;

            son.value = 10;
            b.value = b.value-10;
        }
        //this.CreateSplit(son);

    }
    public ArrayList GetEatBalls(Ball b)
    {
        ArrayList ret = new ArrayList();


        foreach (Ball balls in objs)
        {

            if (balls == b) continue;
            if (b.value < balls.value) continue;
            if (!balls.alive) continue;

            float dis = Utils.ClaculateDistance(new Vector2(balls.x, balls.y), new Vector2(b.x, b.y));



            //    Debug.Log(dis + "        " + b.GetRadis() + "     " + balls.GetRadis());
            if (dis < b.radis - balls.radis)
            {
                ret.Add(balls);
            }
            else
            {

            }

        }
        return ret;
    }


    public void Add(Ball b)
    {
        this.objs.Add(b);
    }
    public void SetSelfRank(int rank)
    {
        this.rank = rank;
    }
    public int GetSelfRank()
    {
        return this.rank;
    }
    public void Remove(Ball b)
    {
        this.objs.Remove(b);

        // AutoReleasePool.ins.AddObject(b);
    }
    public override void OnEvent(int type, object userData)
    {
        Debug.Log("death one ball");
        if (type == Event.ID_ONE_SPLIT_DEATH)
        {
            Ball b = userData as Ball;
            if (b.no == me_no)
            {
                self = this.GetTheMaxOne(me_no);

                Debug.LogError("2222222222222222  +" + self.GetHashCode().ToString() + "   " + b.GetHashCode());


            }
        }
    }

    public BallsMgr()
    {
        EventDispatcher.ins.AddEventListener(this, Event.ID_ONE_SPLIT_DEATH);
    }
}


